import pygame
import spyral
import random
import math

import Dic
import Point
import AI
import Instruction

MARGIN = 80;

class Game(spyral.scene.Scene):
    def __init__(self):
        spyral.scene.Scene.__init__(self)
        self.camera = spyral.director.get_camera();
        self.group = spyral.sprite.Group(self.camera)
        
        self.AI = AI.AI();
        self.moves = None;
        self.index = 0;
        self.instruction = None;
        self.timer = 0;
        self.speed = 20;
        
        self.phase = 0;
        '''
        0: AI make move
        1: Disp "watch"
        2: Show moves
        3: Disp "imitate"
        4: Player make moves
        '''
        
        self.group.add();
    
    
    def on_enter(self):
        bg = spyral.util.load_image(Dic.images['bg'])
        self.camera.set_background(bg)
        

    def render(self):
        self.group.draw()
        self.camera.draw()

   
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
            if (self.phase == 4):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.checkPoint(pygame.mouse.get_pos()) == True):
                        self.group.add(Point.Point(pygame.mouse.get_pos()))
                        self.index += 1;
                    else:
                        spyral.director.pop();
            
                    
        if (self.phase == 0):
            #AI makes a move
            self.AI.makeMove();
            self.moves = self.AI.getMoves();
            self.phase += 1;
        
        elif (self.phase == 1):
            #display "watch"
            if (self.instruction == None):
                self.instruction = Instruction.Instruction(Dic.images['watch'])
                self.group.add(self.instruction)
            else:
                if (self.instruction.checkDone() == True):
                    self.instruction.kill();
                    self.instruction = None;
                    self.phase += 1;
        
        elif (self.phase == 2):
            #show AI moves
            if (self.index < len(self.moves)):
                if (self.timer == 0):
                    self.group.add(Point.Point(self.moves[self.index]))
                    self.timer = self.speed;
                    self.index += 1;
                else:
                    self.timer -= 1;
            elif (self.group.sprites() == []):
                self.phase += 1;
                self.index = 0;
                self.timer = 0;
        
        elif (self.phase == 3):
            #display "imitate"
            if (self.instruction == None):
                self.instruction = Instruction.Instruction(Dic.images['imitate'])
                self.group.add(self.instruction)
            else:
                if (self.instruction.checkDone() == True):
                    self.instruction.kill();
                    self.instruction = None;
                    self.phase += 1;
                    
        
        elif (self.phase == 4):
            #player makes moves
            if (self.index == len(self.moves)):
                self.phase = 0;
                self.index = 0;


        

        self.group.update();
        
        
    def checkPoint(self, point):
        goal = self.moves[self.index];
        print goal;
        print point;
        correct = True;
        if (MARGIN < abs(goal[0] - point[0])):
            correct = False;
        if (MARGIN < abs(goal[1] - point[1])):
            correct = False;
        
        return correct;
        



